<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>商品分类管理</title>
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="/plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="/css/style.css">
	<script src="/plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="/plugins/bootstrap/js/bootstrap.min.js"></script>
	<!--        Toastr提示信息5-->
	<script src="/js/toastr/toastr.min.js"></script>
	<link href="/js/toastr/toastr.css" rel="stylesheet">
	<!--bootstrap-dialog模态框，一般用于增加修改弹出一个窗口-->
	<script src="/js/bootstrap-dialog/dist/js/bootstrap-dialog.js"></script>
	<link href="/js/bootstrap-dialog/dist/css/bootstrap-dialog.css" rel="stylesheet">
    
</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">商品分类管理     
                       	</h3>
                    </div>

                    <div class="box-body">
                  			 <ol class="breadcrumb">	                        	
                        		<li>
		                        	<a href="#" >顶级分类列表</a> 
		                        </li>
		                        <li>
		                       		<a href="#" >珠宝</a>
		                        </li>
		                        <li>
		                        	<a href="#" >银饰</a>
		                        </li>
	                        </ol>

                        <!-- 数据表格 -->
                        <div class="table-box">
							
                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" id="addid" value="" onclick="addticat()" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" class="btn btn-default" title="删除" onclick="deletetcat()" ><i class="fa fa-trash-o"></i> 删除</button>
										<button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                       
                                    </div>
                                </div>
                            </div>                          
                       		
                        
			                <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">分类ID</th>
									      <th class="sorting">分类名称</th>									   
									      <th class="sorting">类型模板ID</th>

					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="tboid">
			                      </tbody>
			                  </table>
			                  <!--数据列表/-->                      
						
                        </div>
                        <!-- 数据表格 /-->
                     </div>
                    <!-- /.box-body -->



  <script>
	  $(function (){

		  getdata()

		  // addticat()

	  })




	  //查询全部
	  function getdata(id){
		  $('#addid').val(id)
		  $.ajax({
			  url:"/tcat/getall",
			  type:"post",
			  dataType:"json",
			  data:{parentId:id},
			  async:false,
			  success:function (result){
				  console.log(result)
				  if(result.code==10000){
					  var ticat=result.data;
					  // console.log(ticat)
					  var html="";
					  for(var i=0;i<ticat.length;i++){
						  html+=' <tr >';
						  html+=' <td><input name="allcheck"  value="'+ticat[i].id+'"   type="checkbox" ></td> >';
						  html+=' <td >'+ticat[i].id+'  </td>';
						  html+='<td>'+ticat[i].name+'</td>';
						  html+=' <td>'+ticat[i].typeId+' </td>';
						  html+=' <td class="text-center">';
						  html+=' <button type="button" class="btn bg-olive btn-xs" onclick="getdata('+ticat[i].id+')" >查询下级</button>';
						  html+=' <button type="button" class="btn bg-olive btn-xs" onclick="updatecat('+ticat[i].id+')" data-toggle="modal" data-target="#updateModal" >修改</button>';
						  html+=' <button type="button" class="btn bg-olive btn-xs" onclick="deletebyid('+ticat[i].id+')" data-toggle="modal" >删除</button>';
						  html+='</td>'
						  html+='</tr>'
					  }
					  $("#tboid").html(html)

				  }
			  },error:function (){
				  alert("系统异常")
			  }
		  })
	  }




	  //增加
	  function addticat(){
	  	var id=$("#addid").val()//父级id
		  // alert(id)
		  // return
		  $.ajax({
			  url:"/tcat/addteat",
			  type:"post",
			  dataType:"json",
			  data:{id:id},
			  async:false,
			  success:function (result){
				  console.log(result)
				  if(result.code==10000){
				  	var datas=result.data
					  $("#upin2").val(datas.name)
					  var html="";
					  html+='<tr>';
					  // html+=' '
					  html+='<td>上级商品分类</td>';
					  html+='<td>'+datas.name+'  <input id="in1" value="'+datas.id+'" type="hidden" >  </td>';
					  html+='</tr>';
					  html+='<tr id="trs2">';
					  html+='<td>商品分类名称</td>';
					  html+='<td id="td1s"><input  class="form-control" placeholder="商品分类名称">  </td>';
					  html+=' </tr>';
					  html+='<tr>';
					  html+='<td>类型模板</td>';
					  html+='<td id="td2s">';
					  html+='<input select2 config="options[\'type_template\']" placeholder="商品类型模板" class="form-control" type="text"/>';
					  html+='</td>';
					  html+='</tr>';
					  $("#tableid").html(html)
				  }
			  },error:function (){
				  alert("系统异常")
			  }
		  })
	  }


	  //增加保存
	  function addbaocun(){
	  	var parentId=$("#in1").val()//父级id
		   var name=$("#td1s").find("input").val()//商品分类名称name
		  var typeId=$("#td2s").find("input").val()//商品分类名称typeId
		  //增加逻辑代码
		  $.ajax({
			  url:"/tcat/addbaocun",
			  type:"post",
			  dataType:"json",
			  data:{parentId:parentId,name:name,typeId:typeId},
			  async:false,
			  success:function (result){
				  if(result.code==10000){
					  toastr.success("增加成功")
					  dialog.close();//更新成功后关闭
					  // getdata(0);
				  }
			  },error:function (){
				  alert("系统异常")
			  }
		  })
	  }


	  //修改回显
	  function updatecat(id){
		  var parentId=$("#addid").val()//父级id
		  // alert(parentId)
		  var name=$("#upin2").val()
		  // alert(name)
		  // return
		  $.ajax({
			  url:"/tcat/updatecat",
			  type:"post",
			  dataType:"json",
			  data:{id:id,parentId:parentId},
			  async:false,
			  success:function (result){
				  if(result.code==10000){
				  	console.log(result.data)
					  var datas=result.data
					  var html="";
					  html+='<tr><td><input id="upid3" value="'+datas.id+'" type="text" > </td></tr> '
					  html+='<tr>';
					  html+='<td>上级商品分类</td>';
					  //
					  html+='<td><input id="upid2"  type="text" ></td>';
					  html+='</tr>';
					  html+='<tr id="">';
					  html+='<td>商品分类名称</td>';
					  html+='<td id="uptd1"><input value="'+datas.name+'" class="form-control" placeholder="商品分类名称">  </td>';
					  html+=' </tr>';
					  html+='<tr>';
					  html+='<td>类型模板</td>';
					  html+='<td id="uptd2">';
					  html+='<input value="'+datas.typeId+'" select2 config="options[\'type_template\']" placeholder="商品类型模板" class="form-control" type="text"/>';
					  html+='</td>';
					  html+='</tr>';
					  $("#uptableid").html(html)
				  }
			  },error:function (){
				  alert("系统异常")
			  }
		  })
	  }


	  //修改保存
	  function updatebaocun(){
		  var parentId=$("#addid").val()//父级id
		  // alert(parentId)
	  	var id=$("#upid3").val()//id
		  var name=$("#uptd1").find("input").val()//商品分类名称name
		  var typeId=$("#uptd2").find("input").val()//商品分类名称typeId
		  // return
		  $.ajax({
			  url:"/tcat/updatebao",
			  type:"post",
			  dataType:"json",
			  data:{id:id,name:name,typeId:typeId,parentId:parentId},
			  async:false,
			  success:function (result){
				  if(result.code==10000){
					  toastr.success("更新成功")
					  dialog.close();//更新成功后关闭
					  // getdata(0);
				  }
			  },error:function (){
				  alert("系统异常")
			  }
		  })
	  }

	  //删除
	  function deletebyid(id){
	  	// alert(id)
		  $.ajax({
			  url:"/tcat/deletebyid",
			  type:"post",
			  dataType:"json",
			  data:{id:id},
			  async:false,
			  success:function (result){
				  if(result.code==10000){
					  toastr.success("删除成功")
					  dialog.close();//更新成功后关闭
					  // getdata(0);
				  }
			  },error:function (){
				  alert("系统异常")
			  }
		  })
	  }


	  //上边删除
	  function deletetcat(){
		  var arr=[];
		  $("[name=allcheck]").each(function(i,v){
			  if($(v).prop("checked")){
				  arr.push($(v).val())
			  }
		  })
		  // alert(arr)
		  // location.href="deleteall?arr="+arr;
		  var specOptionJson=JSON.stringify(arr)//将json格式转换为string格式，传输后台
		  $.ajax({
			  url:"/tcat/deletearr",
			  type:"post",
			  dataType:"json",
			  data:{arr:specOptionJson},
			  async:false,
			  success:function (result){
				  if(result.code==10000){
					  toastr.success("删除成功")
					  // dialog.close();//更新成功后关闭
					  // getdata(0);
				  }
			  },error:function (){
				  alert("系统异常")
			  }
		  })


	  }









  </script>



  <!-- 编辑窗口 -->
  <div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>

<!--				  <input id="upid4" value="" type="text" >-->
				  <h3 id="umyModalLabel">商品分类编辑</h3>
			  </div>
			  <div class="modal-body">

				  <table class="table table-bordered table-striped"  width="800px" id="uptableid">

				  </table>

			  </div>
			  <div class="modal-footer">
				  <button class="btn btn-success" onclick="updatebaocun()" data-dismiss="modal" aria-hidden="true">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>

  <input id="upin2" value="" type="text">

<!-- 增加窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">商品分类编辑</h3>

		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px" id="tableid">

			 </table>				
			
		</div>
		<div class="modal-footer">						
			<button class="btn btn-success" onclick="addbaocun()" data-dismiss="modal" aria-hidden="true">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>
</body>
</html>